package LeetCode;

public class LC_593_ValidSquare {

    public static void main(String[] args) {

    }

    class Solution {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            long[] lengths = {length(p1, p2), length(p2, p3), length(p3, p4), length(p4, p1), length(p1, p3), length(p2, p4)};
            long max = 0, nonMax = 0;
            for (long len : lengths) max = Math.max(max, len);
            int count = 0;
            for (long length : lengths) {
                if (length == max) count++;
                else nonMax = length;
            }
            if (count != 2) return false;
            for (long len : lengths) if (len != max && len != nonMax) return false;
            return true;
        }

        private long length(int[] p1, int[] p2) {
            return (long) Math.pow(p1[0] - p2[0], 2) + (long) Math.pow(p1[1] - p2[1], 2);
        }

    }


    class Solution_2 {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            if (p1[0] == p2[0] && p1[1] == p2[1]
                    || p1[0] == p3[0] && p1[1] == p3[1]
                    || p1[0] == p4[0] && p1[1] == p4[1]) return false;
            int d2 = distSq(p1, p2);
            int d3 = distSq(p1, p3);
            int d4 = distSq(p1, p4);
            if (d2 == d3 && 2 * d2 == d4) {
                int d = distSq(p2, p4);
                return (d == distSq(p3, p4) && d == d2);
            }
            if (d3 == d4 && 2 * d3 == d2) {
                int d = distSq(p2, p3);
                return (d == distSq(p2, p4) && d == d3);
            }
            if (d2 == d4 && 2 * d2 == d3) {
                int d = distSq(p2, p3);
                return (d == distSq(p3, p4) && d == d2);
            }
            return false;
        }

        int distSq(int[] p, int[] q) {
            return (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
        }
    }
}